﻿/*
 * @author: wizardc
 */

using System.IO;
using Dou.Utils;
using DouEditor.Utils;
using UnityEditor;

namespace DouEditor.Resource.Package
{
    /// <summary>
    /// 打 IPA 包
    /// </summary>
    public class PackIpa
    {
        public void Pack(BuildRule buildRule)
        {
            var buildPath = GetBuildPath(buildRule);
            EditorUtil.CreateDirByFilePath(buildPath);

            PlayerSettings.bundleVersion = buildRule.mainVersion;
            PlayerSettings.iOS.buildNumber = buildRule.subVersion.ToString();

            var scenePath = AssetDatabase.GetAssetPath(buildRule.sceneObj);
            if (buildRule.isDev)
            {
                PlayerSettings.enableInternalProfiler = true;
                BuildPipeline.BuildPlayer(new [] { scenePath },
                    buildPath,
                    BuildTarget.iOS,
                    BuildOptions.CompressWithLz4 | BuildOptions.Development | BuildOptions.ConnectWithProfiler);
            }
            else
            {
                PlayerSettings.enableInternalProfiler = false;
                BuildPipeline.BuildPlayer(new [] { scenePath },
                    buildPath,
                    BuildTarget.iOS,
                    BuildOptions.CompressWithLz4);
            }
        }

        private string GetBuildPath(BuildRule buildRule)
        {
            var name = $"{buildRule.desc}_{buildRule.mainVersion}_{buildRule.subVersion}_{buildRule.hotFixedVersion}_{TimeUtil.FormatNowDate("yyyy_MM_dd_HH_mm_ss")}.ipa";
            return Path.Combine(buildRule.packageOutputPath, "IPA", name);
        }
    }
}
